﻿/*---------------------------------------------------------------------------------
-----------------***DaLianQianYi***-----------------------
-----------------作者：大连仟亿科技------------------------------------------------------
-----------------联系作者：www.qianyikeji.com----------------------------------
-----------------时间：2018/5/3 18:14:59------------------------------------------
----------------------------------------------------------------------------------*/


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Donal.Xms.Common;
using Newtonsoft.Json;

namespace Donal.Xms.Web.qianyi.common
{
    public partial class user_industry_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.user_industry().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("industry_group", DTEnums.ActionEnum.View.ToString()); //检查权限
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.user_industry bll = new BLL.user_industry();
            Model.user_industry model = bll.GetModel(_id);

            txtTitle.Text = model.title;
            rblStatus.SelectedValue = model.is_lock.ToString();
            txtSort_id.Text = model.sort_id.ToString();
            txtCode.Text = model.code;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.user_industry model = new Model.user_industry();
            BLL.user_industry bll = new BLL.user_industry();

            model.title = txtTitle.Text;
            model.is_lock = int.Parse(rblStatus.SelectedValue);
            model.sort_id = int.Parse(txtSort_id.Text);
            model.code = txtCode.Text;

            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加行业 " + JsonConvert.SerializeObject(model)); //记录日志
                return true;
            }
            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.user_industry bll = new BLL.user_industry();
            Model.user_industry model = bll.GetModel(_id);

            model.title = txtTitle.Text;
            model.is_lock = int.Parse(rblStatus.SelectedValue);
            model.sort_id = int.Parse(txtSort_id.Text);
            model.code = txtCode.Text;

            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改行业 "+ JsonConvert.SerializeObject(model)); //记录日志
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("industry_group", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("修改内容成功！", "user_industry_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("industry_group", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", string.Empty);
                    return;
                }
                JscriptMsg("添加内容成功！", "user_industry_list.aspx");
            }
        }

    }
}

